package com.epoint.mobileoa.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.epoint.mobileoa.bizlogic.Res;
import com.epoint.mobileoa.tools.LoadedImage;
import com.epoint.mobileoa.tools.SDCard;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;


/**
 * 自定义Adapter
 * 
 * @author lilin
 * @date 2011-12-6 下午12:59:07
 * @ClassName: ListPicAdapter
 */
public class ListPicAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	public static Map<Integer, Boolean> isSelected;
	private ArrayList<LoadedImage> photos = new ArrayList<LoadedImage>();
	List<String> imgPaths;
	int length;
	Context context;

	public ListPicAdapter(Context context, List<String> _imgPaths) {
		this.context = context;
		imgPaths = _imgPaths;
		length = imgPaths.size();
		mInflater = LayoutInflater.from(context);
		// 这儿定义isSelected这个map是记录每个listitem的状态，初始状态全部为false。
		isSelected = new HashMap<Integer, Boolean>();
		for (int i = 0; i < length; i++) {
			isSelected.put(i, false);
		}
	}

	public void addPhoto(LoadedImage photo) {// 把图片添加到数组
		photos.add(photo);
	}

	public int getCount() {// 得到图片数量
		return photos.size();
	}

	public Object getItem(int position) {
		return photos.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	Handler handler = new Handler();

	public View getView(int position, View view, ViewGroup parent) {
		ViewHolder holder = null;
		// view为null的时候初始化view。
		if (view == null) {
			holder = new ViewHolder();
			view = mInflater.inflate(Res.getLayoutInt("listpic"), null);
			holder.filename = (TextView) view.findViewById(Res.getIdInt("filename"));
			holder.imageView = (ImageView) view.findViewById(Res.getIdInt("image"));
			holder.checkBox = (CheckBox) view.findViewById(Res.getIdInt("cb"));

			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		holder.filename.setText(SDCard.getName(imgPaths.get(position)));
		holder.imageView.setImageBitmap(photos.get(position).getBitmap());
		holder.checkBox.setChecked(isSelected.get(position));
		final CheckBox checkBox = holder.checkBox;
		final int arg2 = position;
		checkBox.setChecked(isSelected.get(position));
		checkBox.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// 记录CheckBox的状态
				if (isSelected.get(arg2)) {
					isSelected.put(arg2, false);
				} else {
					isSelected.put(arg2, true);
				}
				notifyDataSetChanged();
			}
		});
		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
			}
		});
		return view;
	}

	public final class ViewHolder {
		public TextView filename;// 文件名
		public CheckBox checkBox;// 复选框
		public ImageView imageView;// 预览图
	}

}
